package service;

import bean.Public;
import dao.PublicInfoMapper;

import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author 李夏
 * @version 1.0
 */
public class Appointmant {
    public void appointment(Integer publicId,String stuName,Integer stuId,Integer publicTime) throws SQLException {
        Timer timer = new Timer();
        PublicInfoMapper publicInfoMapper=new PublicInfoMapper();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Public publicInfo= null;
                try {
                    publicInfo = publicInfoMapper.queryPublic(publicId);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

                publicInfo.setStuName("空闲");

                publicInfo.setStuId(0);
                publicInfo.setPublicTime(0);
                Integer already= null;
                try {
                    already = publicInfoMapper.queryPublicAlready(publicId);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                publicInfo.setPublicAlready(already-1);
                publicInfoMapper.updatePublic(publicInfo);
            }
        },publicTime*60*1000);
        Public publicInfo =publicInfoMapper.queryPublic(publicId) ;
        publicInfo.setPublicId(publicId);
        publicInfo.setStuName(stuName);
        publicInfo.setStuId(stuId);
        publicInfo.setPublicTime(publicTime);
        Integer already = publicInfoMapper.queryPublicAlready(publicId);
        publicInfo.setPublicAlready(already+1);
        publicInfoMapper.updatePublic(publicInfo);

    }
}
